home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16634 < prev    next >
Encoding:
Text File  |  1996-08-05  |  8.9 KB  |  198 lines

  1. Newsgroups: comp.lang.c++
  2. Path: artemis.sto.fdata.se!news
  3. From: Niklas Mellin <niklas.mellin@sto.fdata.se>
  4. Subject: Re: Why don't you use garbage collection
  5. Sender: news@artemis.sto.fdata.se (UseNet NetNews)
  6. Message-ID: <316D291F.465D@sto.fdata.se>
  7. Date: Thu, 11 Apr 1996 15:45:35 GMT
  8. Content-Transfer-Encoding: 7bit
  9. Content-Type: text/plain; charset=us-ascii
  10. References: <4kiai0$mjd@dfw-ixnews8.ix.netcom.com>
  11. Mime-Version: 1.0
  12. X-Mailer: Mozilla 2.0 (WinNT; I)
  13. Organization: WM-data F÷rsvarsdata AB, Sweden
  14.  
  15. Giuliano Carlini wrote:
  16. > I'm a long time proponent of using garbage collection in C and C++
  17. > programs, and I'm curious:
  18. >         - How many others are there?
  19.  
  20. Not me (usually).
  21.  
  22. >         - Why don't most C/C++ programmers use it?
  23.  
  24. I don't use it mostly because I tend to put almost every object I use
  25. on the stack and not on the heap. The heap usage I hide in low level
  26. classes, that typically have some kind of reference counter, and takes
  27. care of the deletions.
  28.  
  29. Sometimes it is necessary to put a high level object on the heap, but
  30. those cases are so rare that it is not worth the trouble implementing
  31. a garbage collector. And personally I usually don't have any problem 
  32. remembering the delete or delete[] in those cases. When there are
  33. resource leaks in my programs it is most often due to forgetting to
  34. return some other resource than memory, in the destructor of an object.
  35.  
  36. > I'm particularly interested in finding out why most C/C++ don't use it.
  37. > While I have my own theories - which I'll describe below - I'm
  38. > interested
  39. > in finding out more directly from those who are against it. I've spent
  40. > a
  41. > good part of the past 4 years trying to convince people to use it, and
  42. > I've got a handle on why the small group of people I associate with are
  43. > not using it, but I'd like to find out more about why the C/C++
  44. > community
  45. > as a whole does not.
  46.  
  47. I am not against it, it is just that I think the stack suits my needs
  48. better. I agree though that there have been cases I have wished I had
  49. a garbage collector. But I don't think a garbage collector fits into
  50. the language as well as in Smalltalk or Lisp. There is just too much
  51. you can do with pointers, so implementing a garbage collecting C++
  52. is anything but trivial, and restricting the usage of pointers would
  53. surely not be biased towards the C++ culture.
  54.  
  55. > I hope that with this information, I'll be able to perfect a more
  56. > convincing presentation for why the default should be to use garbage
  57. > collection, and for why explicit calls to free/delete should be used
  58. > only
  59. > in the rare cases for which garbage collection is inappropriate.
  60.  
  61. There are very few calls to new and delete in the code I write, except
  62. in the implementation of low level classes.
  63.  
  64. > What follows is my belief for why garbage collection is so little used.
  65. > Feel free to respond to anything I say below, but please, first respond
  66. > to the questions above. I believe that most people don't use garbage
  67. > collection because either they:
  68. >         - don't know what it is
  69.  
  70. I think most professional programmers at least have an idea what it is.
  71. Everyone that is majoring in computer science today I believe sees it
  72. in school. 
  73.  
  74. >         - don't know it can be used with C/C++.
  75.  
  76. I haven't seen any implementation that I really liked, but then I
  77. haven't activly searched for one either.
  78.  
  79. >         - are misinformation
  80. >         - are biased against it by the C/C++ culture
  81. > In my experience, most C/C++ programmers either don't know what garbage
  82. > collection is, or don't know that it can be used with C/C++. After all,
  83. > no major C/C++ compiler includes a garbage collector. At least, as far
  84. > as I know. I hope I'm wrong, and that someone can correct me. But even
  85. > after, I tell them what it is, and that it can be used with C++, almost
  86. > everyone still rejects it.
  87.  
  88. No major C/C++ compiler includes a garbage collector, because it is not a
  89. part of the C++ language. If you want a garbage collector you should go out
  90. looking for a class library that implements it, not for a compiler. A C++
  91. compiler that do garbage collection when you forget delete wouldn't be a
  92. C++ compiler.
  93.  
  94. > At first, most offer technical reasons for rejecting it. Almost all are
  95. > based on misinformation, since garbage collection is usable and
  96. > benificial
  97. > for the vast majority of systems. The most often mentioned is that
  98. > garbage
  99. > collection is too inefficient. Either it uses too much CPU, or too much
  100. > memory. Fortunately, this is easy to rebut. The research literature
  101. > gives
  102. > ample evidence that modern garbage collectors for C/C++ use around the
  103. > same CPU time as programs that explicitly call delete/free. My personal
  104. > experience is that typical programs that call delete/free use about the
  105. > same amount of memory as garbage collected programs. In order to impose
  106. > the predictability needed to permit calls to delete, they are forced to
  107. > do deep copies of complex data structures. This uses a great deal of
  108. > memory. Garbage collected programs need use only a single copy of the
  109. > data, and this compensates for the additional memory taken up by
  110. > unreachable objects.
  111.  
  112. My philosophy is that reference counting together with copy on write
  113. solves this problems just as good garbage collection in most cases.
  114.  
  115. > Another reason often given is that garbage collection introduces long
  116. > pause times that are acceptable to users. This was certainly true of
  117. > garbage collectors at one time, but modern collectors allow the client
  118. > program to employ several techniques to eliminate pause times. The most
  119. > sophisticated is to operate in a separate thread. Thus while the
  120. > garbage
  121. > collection thread is executing, the user can still interact with the
  122. > system. Another technique is to run the collector when the user has
  123. > been
  124. > idle for X seconds. If the user has been idle so far, he is likely to
  125. > remain idle. So, the collector runs while the user is making no demands
  126. > on
  127. > the system. If he happens to resume interating with the program in the
  128. > middle of the collection, the collector detects this, and abandons the
  129. > collection. The collector can then wait for the next idle period.
  130. > Other technical reasons are offered, but I'll skip them as this note is
  131. > already much too long. They apply only to unusual applications that are
  132. > outside the mainstream. Even most of these reasons are wrong though.
  133.  
  134. I'd love to take a look at a garbage collector that solves all these
  135. problems in the way you describe above. If you know one for C++, please
  136. give me a pointer to it. (I don't want to implement it myself).
  137.  
  138. > So, we're left with the cultural reasons, and these are I believe the
  139. > "real" reasons why people reject garbage collection. I can shoot down
  140. > incorrect technical arguments, but it's tough to dispute culture,
  141. > although
  142. > I sure try. The crux of cultural reason against garbage collection is
  143. > that
  144. > relying on a garbage collector is "sloppy". The "Code talk" note in the
  145. > January Byte is a great example of this thinking. This sort of C/C++
  146. > programmer believes that it is just wrong to not explicitly call
  147. > delete.
  148. > That if you don't, your an undisciplined hack. That if you don't,
  149. > you've
  150. > turned to the dark side and surely will come to a bad end.
  151.  
  152. Every language has its own culture. Since good garbage collectors
  153. in C++ is if not unexcistent, at least very rare, it is just
  154. natural that not deleting an object is regarded as a bad habit.
  155.  
  156. I refuse to participate at any religous war between different
  157. languages. I realize that every language has its strengths and
  158. weaknesses. I am good at C++, but I know quite a few other
  159. languages too, and appriciate them for their strengths.
  160.  
  161. > I'm not sure how to address this. My current argument seems to get them
  162. > thinking, but isn't immediately convincing. Anyone have any ideas on a
  163. > better approach. Or perhaps on a better presentation. My argument is
  164. > that
  165. > computers are very good at mechanistic, repetitive tasks. And that
  166. > people
  167. > are usually pretty bad at them; being mechanistic and repetitive, these
  168. > tasks are rather boring, which means that our attention often strays.
  169. > People
  170. > routinely turn over such tasks to computers. For example, bookkeeping
  171. > or
  172. > inventory control. And what else is memory deallocation but a sort of
  173. > bookkeeping or inventory control. It's just that rather than being for
  174. > a resource outside of the computer it is for one inside.
  175.  
  176. So why not do as I do? Write low level template classes that takes care
  177. of the book keeping, and reuse them, and don't use the heap more than
  178. necessary, usually it is easier and more efficient to use the stack.
  179.  
  180. > For anyone who may be wondering, I believe that we should use garbage
  181. > collection because:
  182. >         - It vastly decreases the number of bugs in programs which use
  183. > it.
  184.  
  185. It is easier to debug a reference counting class than to debug a garbage
  186. collector.
  187.  
  188. >         - It vastly decreases the time to complete programs which use
  189. > it.
  190. >         - It vastly increases the understandability, reuseability,
  191. >                 extensability, and maintainability of programs which use
  192. > it.
  193.  
  194. ---
  195. Niklas Mellin
  196.